package com.iaiai.browser.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.iaiai.browser.activity.database.DatabaseOpenHelper;
import com.iaiai.browser.activity.entiry.History;

/**
 * 
 * <p>
 * Title: HistoryActivity.java
 * </p>
 * <p>
 * E-Mail: 176291935@qq.com
 * </p>
 * <p>
 * QQ: 176291935
 * </p>
 * <p>
 * Http: iaiai.iteye.com
 * </p>
 * <p>
 * Create time: 2011-12-10
 * </p>
 * 
 * @author 丸子
 * @version 0.0.1
 */
public class HistoryActivity extends ListActivity implements OnScrollListener {

	private DatabaseOpenHelper databaseOpenHelper;

	private HistoryAdapter adapter = new HistoryAdapter();

	private ProgressBar progressBar;

	private int lastItem = 0;

	private List<History> list = new ArrayList<History>();

	/**
	 * 设置布局显示为目标有多大就多大
	 */
	private LayoutParams WClayoutParams = new LinearLayout.LayoutParams(
			LinearLayout.LayoutParams.WRAP_CONTENT,
			LinearLayout.LayoutParams.WRAP_CONTENT);
	/**
	 * 设置布局显示目标最大化
	 */
	private LayoutParams FFlayoutParams = new LinearLayout.LayoutParams(
			LinearLayout.LayoutParams.FILL_PARENT,
			LinearLayout.LayoutParams.FILL_PARENT);

	private void init() {
		databaseOpenHelper = new DatabaseOpenHelper(this);

		// 线性布局
		LinearLayout layout = new LinearLayout(this);
		// 设置布局 水平方向
		layout.setOrientation(LinearLayout.HORIZONTAL);
		// 进度条
		progressBar = new ProgressBar(this);
		// 进度条显示位置
		progressBar.setPadding(0, 0, 15, 0);

		layout.addView(progressBar, WClayoutParams);

		TextView textView = new TextView(this);
		textView.setTextColor(Color.BLACK);
		textView.setText("加载中...");
		textView.setGravity(Gravity.CENTER_VERTICAL);

		layout.addView(textView, FFlayoutParams);
		layout.setGravity(Gravity.CENTER);

		LinearLayout loadingLayout = new LinearLayout(this);
		loadingLayout.addView(layout, WClayoutParams);
		loadingLayout.setGravity(Gravity.CENTER);

		// 得到一个ListView用来显示条目
		ListView listView = getListView();
		// 添加到脚页显示
		listView.addFooterView(loadingLayout);
		//
		registerForContextMenu(listView);
		//
		setListAdapter(adapter);
		listView.setOnScrollListener(this);
		listView.setBackgroundColor(Color.WHITE);	//背景色
		listView.setCacheColorHint(0x00000000);	//滑动时透明
		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(HistoryActivity.this,
						MainActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString("url", list.get(position).getUrl());
				intent.putExtras(bundle);
				setResult(RESULT_OK, intent);

				finish();
			}
		});

		list.addAll(databaseOpenHelper.queryHistory(0, 50));

		if (list.size() < 50) {
			layout.setVisibility(View.GONE);
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		init();
	}

	public void onScrollStateChanged(AbsListView view, int scrollState) {
		if (lastItem == list.size()
				&& scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
			list.addAll(databaseOpenHelper.queryHistory(list.size(),
					list.size() + 50));
			adapter.notifyDataSetChanged();
		}
	}

	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		lastItem = firstVisibleItem + visibleItemCount - 1;
	}

	/**
	 * 要用用于生成显示数据
	 */
	private class HistoryAdapter extends BaseAdapter {

		public int getCount() {
			return list.size();
		}

		public Object getItem(int pos) {
			return pos;
		}

		public long getItemId(int pos) {
			return pos;
		}

		public View getView(int pos, View v, ViewGroup p) {
			TextView view = new TextView(HistoryActivity.this);
			view.setTextColor(Color.BLACK);
			view.setHeight(50);
			view.setText("[" + list.get(pos).getDate() + "]: - "
					+ list.get(pos).getUrl());
			return view;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.history, menu);
		return true;
	}
	
	@Override  
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {  
        case R.id.menu_clear:
        	databaseOpenHelper.clearHistory();
        	Intent intent = new Intent(HistoryActivity.this,HistoryActivity.class);
        	startActivity(intent);
        	finish();
            break;  
        default:  
            break;  
        }  
        return super.onOptionsItemSelected(item);  
    }

}
